<!-- 广告栏招商页 -->
<template>
	<view class="my-content attract">
		<view>
			<buildForm v-if="formLoading" :select_city_new="[]"  :select_city_newd="[]" :value="form" :formItems="formItems" @input="formInput" />
			<view v-if="lang === 'ru'">
				<view class="title">{{title}}</view>
				<view class="subTitle">{{description}}</view>
				<view class="addFiles" @click="chooseFiles" v-if="uploadDataList.length === 0">
					<span>+</span>
					<view>Запись</view>
				</view>
				<view class="filePreview" v-if="videoObj.tempFilePath">
					<video :src="videoObj.tempFilePath" :poster="videoObj.thumbTempFilePath"></video>
					<u-button type="error" @click="deleteUploadList()">Удалить</u-button>
				</view>
			</view>
		</view>
		<view class="button">
			<u-button v-if="flag==0" :custom-style="{
			           width: '656rpx',
			           height: '63rpx',
			           background: 'linear-gradient(110deg, #DD2910 0%, #F79B29 100%)',
			           'border-radius': '26rpx',
			          }" hover-class="none" type="primary" @click="subForm">
				{{ $t('提交') }}
			</u-button>
			<u-button v-if="flag==1" :custom-style="{
			           width: '656rpx',
			           height: '63rpx',
			           background: 'linear-gradient(110deg, #DD2910 0%, #F79B29 100%)',
			           'border-radius': '26rpx',
			          }" hover-class="none" type="primary" >
				{{ $t('提交') }}
			</u-button>
			<!-- 
				 
				 (请对准摄像头 点击开始 录制视频按要求说出以下文字 例:你好 我是俄罗斯伊尔库斯克用户 我想使用GLUZapp向中国采购商品 申请同意)
				 企业 法人姓名身份证号上传一个营业执照手机号(我们在后台审核一下)
				 
				 -->
		</view>
		<!-- 获取权限的功能 -->
		<Permission></Permission>
	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
		FILE_MAX_SIZE,
		FILE_MAX_COUNT
	} from '@/config/app';
	import buildForm from "@/components/buildForm/index.vue"
	import {
		time
	} from '@/common/public.js'
	import {
		mapForm
	} from 'pages/release/buildFormItems.js'
	import {
		attract
	} from './attract.js'
	import Permission from "@/components/Permission.vue"

	export default {
		data() {
			return {
				lang: getApp().globalData.lang,
				site: getApp().globalData.site,
				userInfo: {},
				id: 0,
				flag:0,
				form: {},
				formItems: [],
				formLoading: false, // 加载完成后再引入组件
				uploadDataList: [],
				action: HTTP_REQUEST_URL + '/common/upload',
				videoObj: {},
				title: 'Обязательная функция',
				description: 'Нажмите, чтобы начать запись видео, нацелиться на камеру, по запросу произнести следующий текст, Здравствуйте, я российский пользователь Иркутска, я хочу использовать GluZapp для покупки товаров в Китае, подать заявку на согласие',
			};
		},
		components: {
			buildForm,Permission
		},
		computed: {
			isQy() {
				//验证是否为企业  ,site.identity_id 是个人类型,目前只有一个
				return this.userInfo.identity_id != this.site.identity_id
			}
		},
		async onLoad(options) {
			this.userInfo = await this.$u.api.user.getUserInfo();
			if (this.lang === 'zh' && this.isQy) {
				this.formItems = attract[1]
				mapForm(this.formItems, this.id).then(res => {
					this.form = res
					this.formLoading = true
				})
			}
			if (this.lang === 'zh' && !this.isQy) {
				this.formItems = attract[0]
				mapForm(this.formItems, this.id).then(res => {
					this.form = res
					this.formLoading = true
				})
			}
			if (this.lang === 'ru') {
				this.formItems = attract[2]
				mapForm(this.formItems, this.id).then(res => {
					this.form = res
					this.formLoading = true
				})
			}

		},
		mounted() {},
		onReady() {
			uni.setNavigationBarTitle({
			    title: this.$t("实名认证")
			})
		},
		onShow() {
			this.$u.api.user.is_user().then(r => {})
		},
		methods: {
			subForm() {
				var that = this
				const reg =
					/^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|[7-9]1)\d{4}(19|20|21)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
				if (this.lang === 'zh') {
					if (!reg.test(this.form.idnum)) {
						this.$u.toast('身份证号码错误')
						return;
					}
				}
				this.flag=1
				uni.showLoading({
					mask:true
				})
				this.$u.api.user.authName({
					...that.form,
					video: that.uploadDataList
				}).then(res => {
					that.$u.toast(that.$t('提交成功，请等待后台审核'))
					setTimeout(() => {
						that.$u.route({
							type: "navigateBack"
						})
						uni.hideLoading()
					}, 1000)
					
					setTimeout(function(){
						that.flag = 0
					},2000)
				}).catch(err => {
					that.$u.toast(err.data.msg)
				})
			},
			formInput(val) {
				this.form = val
			},
			chooseFiles() {
				const that = this
				uni.chooseVideo({
					// count: 1,
					sourceType: ["camera"],
					compressed: true,
					success: function(res) {
						uni.showLoading({
							title: that.$t('上传中')
						})
						if (res.size > 52428800) {
							that.$u.toast(that.$t('上传文件过大'))
							return false
						}
						console.log('chooseVideo', res)
						const tempFilePaths = res.tempFilePath
						that.videoObj = res;
						that.uploadFile(tempFilePaths)
					}
				});
			},
			uploadFile(file) {
				const that = this
				uni.uploadFile({
					url: that.action,
					filePath: file,
					name: 'file',
					success: (uploadFileRes) => {
						console.log('uploadFileRes', uploadFileRes)
						uni.hideLoading()
						const resData = JSON.parse(uploadFileRes.data)
						if (resData.code === 1) {
							let resUrl = {
								url: resData.data.url
							}
							that.uploadDataList.push(resUrl)
						} else {
							that.$u.toast(resData.msg)
						}
					}
				});
			},
			deleteUploadList() {
				this.uploadDataList.splice(0, 1)
				this.videoObj = {}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;
	}

	.title {
		font-size: 40rpx;
		text-align: center;
		margin: 30rpx 0;
	}

	.subTitle {
		font-size: 30rpx;
		text-align: center;
		padding: 0 20rpx;
		margin-bottom: 40rpx;
	}

	.addFiles {
		width: 200rpx;
		height: 200rpx;
		border-radius: 50%;
		background: #eee;
		margin: 0 auto;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
		margin-bottom: 50rpx;

		span {
			width: 100%;
			text-align: center;
			font-size: 60rpx;
		}

		view {
			position: relative;
			top: -15%;
		}
	}

	.filePreview {
		margin: 50rpx 0;

		video {
			width: 100%;
		}
	}
</style>